﻿using System;
using System.Reflection;
using AOPDemo.Domain;
using AOPDemo.Interfaces;
using AOPDemo.Services;
using PostSharp.Aspects;

namespace AOPDemo.Aspects
{
    [Serializable]
    public class LogErrors : OnExceptionAspect
    {
        [NonSerialized] private ILoggingService loggingService;

        public override void RuntimeInitialize(MethodBase method)
        {
            loggingService = new ConsoleLoggingService();
        }

        public override void OnException(MethodExecutionArgs args)
        {
            loggingService.LogMessage(LogLevel.Error, args.Exception.ToString());
        }
    }
}